package ca.shojaei.mini_sat;

import java.util.ArrayList;

public class Model2 {
	
	//ArrayList<State2> states = new ArrayList<State2>();
	private Cell[][] model = new Cell[5][5];


	public Model2(){
		
		Cell state;
		
		char ch = 'a';
		
		for(int i = 0; i <5; i++){
			for (int j = 0; j<5; j++){
				
				state = new Cell(getNeighbors(i,j), ch);
				model[i][j] = state;
				ch = (char)(ch +1);
			}
		}
		
		//currentState = new ArrayList<Cell>();
	}
	
	public ArrayList<int[]> getNeighbors(int row, int col){
		
		ArrayList<int[]> neighbors = new ArrayList<int[]>();
		
		int[] n = new int[2];
		
		//top left
		n[0] = boundNumber(row,-1);
		n[1] = boundNumber(col,-1);
		neighbors.add(n);
		
		//top 
		n[0] = boundNumber(row,-1);
		n[1] = col;
		neighbors.add(n);
		
		//top right
		n[0] = boundNumber(row,-1);
		n[1] = boundNumber(col,1);
		neighbors.add(n);
		
		//right
		n[0] = row;
		n[1] = boundNumber(col,1);
		neighbors.add(n);
		
		//bottom right
		n[0] = boundNumber(row,1);
		n[1] = boundNumber(col,1);
		neighbors.add(n);
		
		//bottom
		n[0] = boundNumber(row,1);
		n[1] = col;
		neighbors.add(n);
		
		//bottom left
		n[0] = boundNumber(row,1);
		n[1] = boundNumber(col,-1);
		neighbors.add(n);
		
		//left
		n[0] = row;
		n[1] = boundNumber(col,-1);
		neighbors.add(n);
		
		return neighbors;
		
	}
	
	public int boundNumber(int i, int j){
		
		int r = i + j;
		if(r < 0)
			r = 4;
		if(i > 4)
			r = 1;
		return r;
	}

	public Cell[][] getStates() {
		return model;
	}

	public void setStates(Cell[][] states) {
		this.model = states;
	}

	public void setInitialStates(ArrayList<int[]> initial){
		
		for(int i = 0; i < initial.size(); i++){
			model[initial.get(i)[0]][initial.get(i)[1]].setAlive(true);
		}
	}

	public void printModel(){
		
		String str= "";
		for(int i = 0; i <5; i++){
			for (int j = 0; j<5; j++){
				System.out.print(model[i][j].getVar() + " ");
			}
			System.out.println("");
		}
	}

	public Cell[][] getNextState(){
		
		Cell[][] newStates =  new Cell[5][5];
		for(int i = 0; i <5; i++){
			for (int j = 0; j<5; j++){
					newStates[i][j].setAlive(!cellDies(i,j));
			}
		}
		
		return newStates;
	}
	
	public ArrayList<Cell> getNextStateTrueVars(){
		
	ArrayList<Cell> nextTrue = new ArrayList<Cell>();
	Cell[][] newStates =  getNextState();
		for(int i = 0; i <5; i++){
			for (int j = 0; j<5; j++){
					if(newStates[i][j].isAlive())
						nextTrue.add(newStates[i][j]);
			}
		}
		
		return nextTrue;
	}

	public boolean cellDies(int row, int col){
		
		ArrayList<int[]> neighbors = model[row][col].getNeighbors();
		int deadN = 0;
		int liveN = 0;
		int [] n;
		
		for(int i = 0; i < 8; i++){
			n = neighbors.get(i);
			if((model[n[0]][n[1]]).isAlive())
				liveN++;
			else
				deadN++;
		}
		
		return (liveN < 2 || liveN > 3)? true : false;
	}
	
	public int[] varPos(char a){
		
		boolean found = false;
		int[] pos = new int[2];
		int i = 0;
		int j = 0;
		while(!found){
			while(!found){
				if(model[i][j].getVar() == a)
					found = true;
				else
					j++;
			}
			if(!found)
				i++;
		}
		return pos;
	}
}
